package com.scott.crm.holiday.service.impl;

import com.scott.crm.clazz.bean.ClazzStudentTeacherBean;
import com.scott.crm.clazz.dao.ClazzStudentTeacherDao;
import com.scott.crm.holiday.bean.HolidayBean;
import com.scott.crm.holiday.dao.HolidayDao;
import com.scott.crm.holiday.dto.HolidayDto;
import com.scott.crm.holiday.service.HolidayService;
import com.scott.crm.user.bean.UserBean;
import com.scott.crm.user.dao.UserDao;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王伟雄
 * @create 2021/11/30 14:49
 */
@Service
public class HolidayServiceImpl implements HolidayService {

    @Autowired
    private HolidayDao dao;
    @Autowired
    private ClazzStudentTeacherDao clazzStudentTeacherDao;
    @Autowired
    private UserDao userDao;
    @Override
    public Integer addByStudent(HolidayBean holidayBean) {
        UserBean userBean=new UserBean();
        userBean.setId(holidayBean.getStudentId());
        // 通过前端传来的名字查到用户id，用户id与学生id相同
        if(userDao.selectOne(userBean)==null){
            return 0;
        }
        // 把获取到的学生id放入bean中
        holidayBean.setFlag(900100);
        return dao.insert(holidayBean);
    }

    @Override
    public Integer deleteByStudent(HolidayBean holidayBean) {
        if(dao.select(holidayBean).size()==0){
            return 0;
        }
        return dao.delete(holidayBean);
    }

    @Override
    public Integer updateByTeacher(HolidayDto dto) {
        List<Integer> ids = dto.getIds();
        Integer flag = dto.getFlag();
        for(Integer id:ids){
            HolidayBean bean = dao.selectByPrimaryKey(id);
            int flaG = bean.getFlag();
            if(flaG==flag){
                return 0;
            }
        }
        return dao.updateByTeacher(ids,flag);
    }

    @Override
    public List<HolidayBean> findByStudent(Integer begin,Integer end,Integer id) {
        System.out.println(dao.findByStudentLimit(id,begin,end));
        return dao.findByStudentLimit(begin,end,id);
    }

    @Override
    public List<HolidayBean> findByStudent(Integer id) {
        return dao.findByStudent(id);
    }

    @Override
    public List<HolidayBean> findByTeacher(HolidayBean holidayBean) {
        ClazzStudentTeacherBean clazzStudentTeacherBean=new ClazzStudentTeacherBean();
        List<HolidayBean> holidayBeans=new ArrayList<>();
        Integer id = holidayBean.getStudentId();
        clazzStudentTeacherBean.setUserId(id);
        ClazzStudentTeacherBean bean = clazzStudentTeacherDao.selectOne(clazzStudentTeacherBean);
        if(bean==null){
            return holidayBeans;
        }
        Integer clazzId = clazzStudentTeacherDao.selectOne(clazzStudentTeacherBean).getClazzId();
        holidayBean.setClassId(clazzId);
        holidayBeans = dao.findByTeacher(holidayBean);
        // 通过班级，查出班级所在的学生的请假情况
        return holidayBeans;
    }

    @Override
    public List<HolidayBean> findByTeacherLimit( Integer begin, Integer end, HolidayBean holidayBean) {
        ClazzStudentTeacherBean clazzStudentTeacherBean=new ClazzStudentTeacherBean();
        Integer id = holidayBean.getStudentId();
        List<HolidayBean> holidayBeans=new ArrayList<>();
        clazzStudentTeacherBean.setUserId(id);
        ClazzStudentTeacherBean bean = clazzStudentTeacherDao.selectOne(clazzStudentTeacherBean);
        if(bean==null){
            return holidayBeans;
        }
        Integer classId = clazzStudentTeacherDao.selectOne(clazzStudentTeacherBean).getClazzId();
        holidayBean.setClassId(classId);
        holidayBeans = dao.findByTeacherLimit(begin,end, holidayBean);
        // 通过班级，查出班级所在的学生的请假情况
        return holidayBeans;
    }
}
